package com.module.content.ui.info.adapter

import android.graphics.Bitmap
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import com.allen.app.data.bean.InfoBean
import com.allen.app.data.bean.ProviderBean
import com.allen.base.base.basic.extras.OnImageLoadListener
import com.allen.base.base.basic.extras.load
import com.allen.base.utils.AppUtils
import com.allen.base.utils.BlurBitmapUtil
import com.allen.base.utils.ViewUtils
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.module.content.R
import com.module.content.helper.ImageCalculateHelper
import com.module.content.helper.InfoVideoPlayHelper

/**
 * author : Allen
 * date   : 2020/08/14
 * time   : 13:41
 * desc   : 糗事 视频适配器
 */
class InfoVideoAdapter : InfoBaseAdapter() {

    override val itemViewType: Int = ProviderBean.PROVIDER_INFO_VIDEO

    override fun getContentView(): Int = R.layout.info_adapter_info_video

    private var infoVideoPlayHelper: InfoVideoPlayHelper? = null

    override fun convertChild(helper: BaseViewHolder, adapterPosition: Int, item: InfoBean) {
        if (infoVideoPlayHelper == null) infoVideoPlayHelper = InfoVideoPlayHelper(context)
        val contentLayout = helper.getView<FrameLayout>(R.id.contentLayout)
        val imageSize = ImageCalculateHelper.getImageSize(item.image_size?.m)
        //是否是竖屏
        val isPortrait = imageSize?.height ?: 0 > imageSize?.width ?: 0
        if (imageSize != null && !isPortrait) {
            contentLayout.layoutParams?.width = ViewGroup.LayoutParams.MATCH_PARENT
            contentLayout.layoutParams?.height = imageSize.height
        } else {
            contentLayout.layoutParams?.width = ViewGroup.LayoutParams.MATCH_PARENT
            contentLayout.layoutParams?.height = ViewUtils.dp2px(350)
        }

        val backgroundIv = helper.getView<ImageView>(R.id.backgroundIv)
        val backgroundOriginalIv = helper.getView<ImageView>(R.id.backgroundOriginalIv)

        backgroundOriginalIv.load(item.pic_url, 4f, listener = object : OnImageLoadListener {
            override fun onSuccess(bitmap: Bitmap?) {
                if (isPortrait) {
                    val blurBitmap = BlurBitmapUtil.blurBitmap(AppUtils.getContext(), bitmap, 15f)
                    backgroundIv.setImageBitmap(blurBitmap)
                } else {
                    backgroundIv.setImageResource(0)
                }
            }
        })

        helper.getView<View>(R.id.playerIv)
            .setOnClickListener {
                setVideoPlayer(contentLayout, item.low_url, adapterPosition, true)
            }
    }

    fun setVideoPlayer(
        contentLayout: FrameLayout?,
        mediaPath: String?,
        position: Int,
        play: Boolean = false
    ) {
        infoVideoPlayHelper?.setVideoPlayer(contentLayout, mediaPath, position, play)
    }

    fun stop() {
        infoVideoPlayHelper?.stop()
    }

    fun onChildViewDetachedFromWindow(view: View) {
        infoVideoPlayHelper?.onChildViewDetachedFromWindow(view)
    }
}